
function loadStaffData() {
    var url = '/trips/' + $('#trip_id').attr('value') + '/staff_data/';
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        success: function(resp) {
            $.each(resp.waypoints, function(i, wp) {
                var waypoint = $("#wp" + wp.wp_id);
                $("input.add-tag", waypoint).select2("val", wp.tags.split(","));
            });
        }
    });
}

$(document).ready(function() {
    var waypoints = $(".waypoint");

    var html= '员工功能:<span class="timezone"></span> <a class="wp-anchor" href="">锚点</a>';
    var addTagWidget = $('<br><label>加标签: </label><input type="hidden" class="add-tag" style="width:400px;"><input class="save-tags" type="button" value="保存"><span class="resp"></span>');

    $(".staff-operation", waypoints).html(html).show();

    $(".staff-operation", waypoints).each(function () {
        var timestamp = $(this).attr("data-date_added");
        var timezone = $(this).attr("data-timezone");
        var wpid = $(this).attr("data-wpid");
        var trip_id = $(this).attr("data-trip_id");
        var photo = $(this).attr("data-photo");
        $(".timezone", $(this)).text(timestamp + "@" + timezone);
        $(".wp-anchor", $(this)).attr("href", "#wp" + wpid);
        if(photo) {
            $(this).append($('<span>|<span> <a href="/trips/' + trip_id + "/waypoints/" + wpid + '/rotate/?deg=90">逆时针旋转90度</a>'
                    + '<span>|<span> <a href="/trips/' + trip_id + "/waypoints/" + wpid + '/rotate/?deg=270">顺时针旋转90度</a>'));
        }
    });

    $(".staff-operation", waypoints).append(addTagWidget);

    $("input.add-tag", waypoints).select2({
        tags: ['feed排期'],
        tokenSeparators: ["，", ",", " "]
    });
    loadStaffData();

    $(".save-tags", waypoints).on("click", function(e) {
        var _this = $(this);
        var waypoint = _this.parents(".waypoint");
        var tags = $("input.add-tag", waypoint).val();
        var tripId = waypoint.attr("data-trip_id");
        var waypointId = waypoint.attr("data-waypoint_id");
        var url = "/trips/" + tripId + "/waypoints/" + waypointId + "/tagging/";

        data = bt.misc.initPostData();
        data['tags'] = tags;

        $.ajax({
            url: url,
            data: data,
            type: "POST",
            dataType: "json",
            success: function(resp) {
                if(resp.success) {
                    waypoint.find("span.resp").text("成功");
                } else {
                    waypoint.find("span.resp").text("失败");
                }
                waypoint.find("span.resp").fadeIn().delay(2000).fadeOut();
            }
        });
        return false;
    });
});
